package taller2.controller.listeners.GraphCreator;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import taller2.controller.listeners.MouseHandler;
import taller2.controller.modes.AddMode;
import taller2.controller.modes.AddTagMode;
import taller2.controller.modes.ConnectMode;
import taller2.controller.modes.DeleteAllMode;
import taller2.controller.modes.DeleteConnectionsMode;
import taller2.controller.modes.DeleteNodeMode;
import taller2.controller.modes.MoveMode;
import taller2.controller.modes.RandomGraphMode;
import taller2.interfaces.AlgorithmApplicator;
import taller2.interfaces.Graph;
import taller2.utils.Constants;
import taller2.view.draws.DrawingArea;

public class GraphCreatorMenuListener implements ActionListener{
	private DrawingArea area;
	private String availableOptions[];
	protected Graph graph;
	protected AlgorithmApplicator algorithmApplicator;
	
	// constructor
	public GraphCreatorMenuListener(DrawingArea area,Graph graph , AlgorithmApplicator algorithmApplicator){
		this.area = area;
		this.graph = graph;
		this.algorithmApplicator = algorithmApplicator;
		this.setAvailableOptions(Constants.availableOptions);
		
	}
	
	// detects when user changes the mode
	public void actionPerformed(ActionEvent arg0) {        
    	MouseHandler handler = (MouseHandler) area.getMouseListeners()[0];
    	Component comp = (Component) arg0.getSource();
    	handleMenu(handler,(JComboBox<?>) comp);
	}
	
	// handles the event if it is a menu change
	protected void handleMenu(MouseHandler handler,JComboBox<?> menu){
        String selected = (String) menu.getSelectedItem();
        
		if(selected.equals(getAvailableOptions()[0])){
			handler.ChangeMode(new MoveMode());
		}else if(selected.equals(getAvailableOptions()[1])){
			handler.ChangeMode(new AddMode(graph));
		}else if(selected.equals(getAvailableOptions()[2])){
			handler.ChangeMode(new AddTagMode());
		}else if(selected.equals(getAvailableOptions()[3])){
				handler.ChangeMode(new ConnectMode(graph));
		}else if(selected.equals(getAvailableOptions()[4])){
			handler.ChangeMode(new RandomGraphMode(graph));	
		}else if(selected.equals(getAvailableOptions()[5])){
				handler.ChangeMode(new DeleteNodeMode(graph));
		}else if(selected.equals(getAvailableOptions()[6])){
				handler.ChangeMode(new DeleteConnectionsMode(graph));
		}else if(selected.equals(getAvailableOptions()[7])){
				handler.ChangeMode(new DeleteAllMode(graph));
		}

	}

	public String[] getAvailableOptions() {
		return availableOptions;
	}

	protected void setAvailableOptions(String availableOptions[]) {
		this.availableOptions = availableOptions;
	}
	
}
